package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.config.FreemarkerUtil;
import com.bwie.pojo.Park;
import com.bwie.pojo.Rese;
import com.bwie.pojo.Type;
import com.bwie.service.ParkService;
import com.bwie.service.ReseService;
import com.bwie.service.ShowService;
import com.bwie.utils.R;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @Auther:${涛}
 * @Date:2024/9/4 14:28
 * @Description: ReseController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/park/rese")
public class ReseController {

    @Autowired
    private ReseService reseService;

    @Autowired
    private ParkService parkService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ShowService showService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/parktuList")
    public R parktuList() {
        List<Park> list = parkService.parktuList();
        return R.OK(list);
    }


    //预约表
    @PostMapping("/ReseList")
    public R ReseList() {
        List<Rese> list = reseService.list();
        return R.OK(list);
    }



    //预约信息添加表
    @PostMapping("/reseAdd")
    public R reseAdd(@RequestBody Rese rese) {
        reseService.save(rese);


        //发送MQ消息
        String msgId = UUID.randomUUID().toString();

        stringRedisTemplate.opsForValue().set(msgId,msgId+"");
        rabbitTemplate.convertAndSend("day0904","",msgId);

        return R.OK("添加成功");
    }

    //消费

    @RabbitListener(queues = "0904")
    public void setStringRedisTemplate(String msg, Message message, Channel channel) throws IOException {
        System.out.println("传入的值为：msg"+msg);

        try {
            String s = stringRedisTemplate.opsForValue().get(msg);

            if(StringUtils.isEmpty(msg)) {
                System.out.println("已经消费过不能重复消费");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            }else{
                System.out.println("消费成功");



                List<Park> list = parkService.parktuList();
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("list",list);


                String url="D:\\test\\file\\c"+".html";
                FreemarkerUtil freemarkerUtil = new FreemarkerUtil();

                freemarkerUtil.createHtml(hashMap,"ParkList",url);

                channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
                stringRedisTemplate.delete(msg);


            }


        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            throw new RuntimeException(e);
        }
    }


    @PostMapping("/showAdd")
    public R showAdd(@RequestBody Type show) {
        showService.save(show);

        //修改rese表中的状态
        LambdaQueryWrapper<Rese> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Rese::getId,show.getReseId());

        Rese one = reseService.getOne(wrapper);

        one.setStatus(show.getStatus());

        reseService.updateById(one);


        return R.OK("修改成功");

    }



}


